package com.ty.zls.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lxj.xpopup.XPopup;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.ty.zls.R;
import com.ty.zls.bean.Account;
import com.ty.zls.components.MyPopUp;
import com.ty.zls.task.BaseTask;
import com.ty.zls.util.HttpUtil;
import com.ty.zls.util.StringUtils;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class SearchActivity extends AppCompatActivity implements View.OnClickListener {

    @ViewInject(R.id.search_topbar)
    private QMUITopBarLayout search_topbar;
    @ViewInject(R.id.et_search_username)
    private EditText et_search_username;
    @ViewInject(R.id.et_search_id)
    private EditText et_search_id;
    @ViewInject(R.id.et_search_tel)
    private EditText et_search_tel;
    @ViewInject(R.id.bt_search_submit)
    private Button bt_search_submit;
    private QMUITipDialog tipDialog;
    Handler handler = new Handler();
    Map<String, String> request = new HashMap<>();
    private int flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        QMUIStatusBarHelper.translucent(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        x.view().inject(this);

        initTabBar();
        initCompent();
    }

    private void initCompent() {
        flag = getIntent().getIntExtra("flag",0);
        bt_search_submit.setOnClickListener(this);
    }


    private void initTabBar() {
        search_topbar.setBackgroundColor(getResources().getColor(R.color.app_color_theme_7));
        search_topbar.setTitle("查询");
        search_topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SearchActivity.this.finish();
            }
        });
    }


    @Override
    public void onClick(View v) {
        String id = et_search_id.getText().toString();
        String tel = et_search_tel.getText().toString();
        String name = et_search_username.getText().toString();
        if (StringUtils.isEmpty(id) && StringUtils.isEmpty(name) && StringUtils.isEmpty(tel)) {
            showtip("请填写查询条件后查询",QMUITipDialog.Builder.ICON_TYPE_INFO);
            return;
        }
        request.put("accountId", id);
        request.put("name", name);
        request.put("tel", tel);
        new BaseTask(HttpUtil.SEARCH, this, new BaseTask.SuccessCallback() {
            @Override
            public void excute(JSONObject result) {
                JSONArray accounts = result.optJSONArray("accounts");
                if(accounts != null && accounts.length() > 0){
                    List<Account> list = new ArrayList<>();
                    for (int i = 0; i<accounts.length();i++){
                        JSONObject jsonObject = accounts.optJSONObject(i);
                        Account account = new Account();
                        account.setArea(jsonObject.optString("area"));
                        account.setId(jsonObject.optString("id"));
                        account.setArreage(jsonObject.optString("arreage"));
                        account.setBalance(jsonObject.optString("balance"));
                        account.setName(jsonObject.optString("name"));
                        account.setTel(jsonObject.optString("tel"));
                        list.add(account);
                    }
                    XPopup.get(SearchActivity.this)
                            .asCustom(new MyPopUp(SearchActivity.this,list,flag))
                            .show();
                }else{
                    showtip("未找到相关用户",QMUITipDialog.Builder.ICON_TYPE_INFO);
                }


            }
        }).execute(request);

    }


    private void showtip(String title,int type){
        tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(type)
                .setTipWord(title)
                .create();
        tipDialog.show();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                tipDialog.dismiss();
            }
        }, 1500);
    }
}
